//
//  NewsCellFootView.m
//  SimulationTechnologyHeadlines
//
//  Created by 移云 on 2019/10/29.
//  Copyright © 2019 admin. All rights reserved.
//

#import "NewsCellFootView.h"
#import <Masonry/Masonry.h>
@interface NewsCellFootView()

//@property(nonatomic,strong)UIButton *loadMoreButton;
//@property(nonatomic,strong)UIActivityIndicatorView *actIndicatorView;
//@property(nonatomic,strong)UILabel *loadingLabel;

@end


@implementation NewsCellFootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *loadMoreButton = [[UIButton alloc]init];
        self.loadMoreButton = loadMoreButton;
        loadMoreButton.backgroundColor = [UIColor lightGrayColor];
        loadMoreButton.layer.cornerRadius = 6;
        loadMoreButton.layer.masksToBounds = true;
        [loadMoreButton setTitle:@"点击加载更多" forState:UIControlStateNormal];
        [loadMoreButton addTarget:self action:@selector(clickLoadMoreAct:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loadMoreButton];
        [loadMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            //设置内间距
            make.edges.mas_equalTo(UIEdgeInsetsMake(10, 20, 10, 20));
        }];
        
        UILabel *loadingLabel = [[UILabel alloc]init];
        self.loadingLabel = loadingLabel;
        loadingLabel.text = @"正在拼命加载...";
        loadingLabel.textColor = [UIColor lightGrayColor];
        loadingLabel.hidden = true;
        [self addSubview:loadingLabel];
        [loadingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self);
        }];
        
        UIActivityIndicatorView *actIndicatorView = [[UIActivityIndicatorView alloc]init];
        self.actIndicatorView = actIndicatorView;
        actIndicatorView.hidden = true;
        [self addSubview:actIndicatorView];
        [actIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(10);
            make.right.equalTo(loadingLabel.mas_left).offset(-10);
            make.centerY.equalTo(loadingLabel.mas_centerY);
        }];
        
        
        
        
        
    }
    return self;
}

-(void)clickLoadMoreAct:(id)sender
{
    self.loadMoreButton.hidden = true;
    self.loadingLabel.hidden = false;
    [self.actIndicatorView startAnimating];
    
    //延迟执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //调用代理方法为保证不出错，需要先判断一下代理对象是否真的实现了这个方法
        if ([self.delegate respondsToSelector:@selector(newsCellFootViewUpdata:)])
        {
            [self.delegate newsCellFootViewUpdata:self];
        }
        
    });
}


@end
